package com.example.administrator.fragarmentdemotest_a.fragarment_demo.frag;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.fragarmentdemotest_a.R;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.MoreActivity;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.adapter.RvAdapter;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.adapter.RvMoreAdapter;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.dao.FeatureDao;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.Bean.FeatureBean;
import com.example.administrator.fragarmentdemotest_a.utils.app.BaseFragment;

import java.util.Collections;
import java.util.List;

public class MainFragment_C extends BaseFragment implements View.OnClickListener {
    private ImageView iv_head;
    private TextView tv_more;
    private RecyclerView rv;
    private Boolean canMove = false;
    private List<FeatureBean> listData;
    private RvAdapter adapter;
    private FeatureDao dao;
    private List<FeatureBean> close;
    private List<FeatureBean> open;
    private RvMoreAdapter adaptera;

    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        View view = inflater.inflate(R.layout.frag_c, container, false);
        iv_head = view.findViewById(R.id.lv_head);
        tv_more = view.findViewById(R.id.tv_more);
        rv = view.findViewById(R.id.rv_cc);

        return view;
    }

    @Override
    protected void initData() {
        dao = new FeatureDao();
        listData = dao.findAllShow();
        adapter = new RvAdapter(listData);
        rv.setLayoutManager(new LinearLayoutManager(act));
        rv.setAdapter(adapter);


        ItemTouchHelper.Callback callback = new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                if (canMove)
                    return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN
                            , ItemTouchHelper.LEFT);
                else return 0;
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                int from = viewHolder.getLayoutPosition();
                int to = target.getLayoutPosition();
                Collections.swap(listData, from, to);
                adapter.notifyItemMoved(from, to);
                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                int position = viewHolder.getLayoutPosition();
                listData.remove(position);
                adapter.notifyItemRemoved(position);
            }
        };
        new ItemTouchHelper(callback).attachToRecyclerView(rv);


    }

    @Override
    protected void initListener() {
        tv_more.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (tv_more == v) {
            jumpToActivity(MoreActivity.class);
        }
    }
}
